#include<iostream>
using namespace std;
const int N = 10100;
int f[N][N];
int a[N];
int n;
int main()
{
	cin >> n;
	for (int i = 1;i <= n;i++)
	{
		cin >> a[i];
	}
	for (int len = 1;len <= n;len++)
	{
		for (int i = 1;i + len - 1 <= n;i++)
		{
			int j = i + len - 1;
			int t = n - len + 1;//n-len是已经过的天数，我们要的是下一天

			f[i][j] = max((f[i + 1][j] + a[i] * t), (f[i][j - 1] + a[j] * t));
		}
	}
	cout << f[1][n] << endl;
	return 0;
}